<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
  <wx-tables
    :columns="columns"
    title="景区资讯"
    :data-source="dataSource"
    :loading="loading"
    :total="total"
    :size.sync="size"
    :page.sync="page"
    @handlePage="handlePage"
  >
    <!--      <template #search>-->
    <!--        &lt;!&ndash;        <tip-block tip="搜索" />&ndash;&gt;-->
    <!--        <el-form label-position="left" style="margin-top: 10px;display: flex;flex-wrap:wrap;justify-content: space-between">-->
    <!--          <el-form-item style="display: flex;" label="学生姓名" prop="search_code">-->
    <!--            <el-input v-model="searchForm.search_words" clearable placeholder="请输入" />-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="请假单状态" prop="search_code">-->
    <!--            <el-select v-model="searchForm.leave_status_str" clearable placeholder="请选择">-->
    <!--              <el-option-->
    <!--                v-for="(item, type) of LEAVE_STATUS_LIST"-->
    <!--                :key="type"-->
    <!--                :label="item.title"-->
    <!--                :value="item.value"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在学部" prop="search_code">-->
    <!--            <el-select v-model="searchForm.year_department_id" clearable placeholder="请选择学部">-->
    <!--              <el-option-->
    <!--                v-for="item in select_dept"-->
    <!--                :key="item.type"-->
    <!--                :label="item.label"-->
    <!--                :value="item.type"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在年级" prop="search_code">-->
    <!--            <el-select v-model="searchForm.search_grade_name" clearable placeholder="请选择年级">-->
    <!--              <el-option-->
    <!--                v-for="item of 20"-->
    <!--                :key="item"-->
    <!--                :label="item"-->
    <!--                :value="item"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item style="display: flex;" label="所在班级" prop="search_code">-->
    <!--            <el-select v-model="searchForm.search_class_name" clearable placeholder="请选择班级">-->
    <!--              <el-option-->
    <!--                v-for="item of 20"-->
    <!--                :key="item"-->
    <!--                :label="item"-->
    <!--                :value="item"-->
    <!--              />-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--          <div>-->
    <!--            <el-button type="primary" class="table-btn" icon="el-icon-search" @click="handleSearchClick">筛选</el-button>-->
    <!--            <el-button class="table-btn" icon="el-icon-refresh" @click="handleRectSearch">重置</el-button>-->
    <!--          </div>-->
    <!--        </el-form>-->
    <!--      </template>-->

    <!-- tools -->
    <template #tools>
      <el-button type="primary" class="table-btn" @click="handleOpenForm()">新增资讯</el-button>
      <!--      <el-button class="table-btn" icon="el-icon-refresh-right" @click="handleGetTable">刷新</el-button>-->
    </template>
    <template v-slot:content="record">
      <div style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
        <span> {{record.row.content}} </span>
      </div>
    </template>
    <template v-slot:status="record">
      <el-switch
        v-model="record.row.status"
        :active-value="1"
        :inactive-value="0"
        active-color="#409EFF"
        inactive-color="#C0CCDA"
        @change="handleSetStatus(record.row)">
      </el-switch>
    </template>
    <template v-slot:oper="record">
      <div class="oper-box">
        <span><el-link type="primary" @click="handleEit(record.row)">编辑</el-link></span>
        <el-popconfirm
          title="确定删除当前资讯吗？"
          @confirm="handleDelete(record.row.id)"
        >
          <el-link slot="reference" type="danger">删除</el-link>
        </el-popconfirm>
      </div>
    </template>
    <wx-dialog
      ref="wx-dialog"
      :visible.sync="formVisible"
      :is-add="isAdd"
      width="960px"
      @ok="handleSaveForm"
    >
      <div class="wx-dialog-form">
        <el-form ref="ruleForm" v-loading="formLoading" :rules="ruleForm" :model="formInfo" label-position="left" label-width="90px">
          <el-form-item label="景区列表" prop="scenic_id">
            <el-select v-model="formInfo.scenic_id" clearable placeholder="请选择景区">
              <el-option
                v-for="item of scenicList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="资讯标题" prop="title">
            <el-input v-model="formInfo.title" style="width: 200px;" placeholder="请输入详情内容" />
          </el-form-item>
          <el-form-item label="简介" prop="content">
            <el-input type="textarea" v-model="formInfo.content" style="width: 200px;" placeholder="请输入简介" />
          </el-form-item>
          <el-form-item label="详情内容" prop="content_html">
            <Tinymce v-if="formVisible" v-model="formInfo.content_html"  :height="300"/>
          </el-form-item>
        </el-form>
      </div>
    </wx-dialog>
  </wx-tables>
</template>

<script>
import WxDialog from '@/components/WxDialog'
import WxTables from '@/components/WxTables'
import TableMixins from '@/components/WxTables/mixins'
import Tinymce from '@/components/Tinymce/Tinymce'
import {
  tourScenicActicleCreate,
  tourScenicActicleDestroy,
  tourScenicActicleEdit,
  tourScenicActicleGetListByCon, tourScenicActicleSetStatus, tourScenicGetListByCon
} from '@/api/scenic'
export default {
  name: 'ScenicInformation',
  components: {
    WxDialog,
    WxTables,
    Tinymce
  },
  mixins: [TableMixins],
  data() {
    return {
      pwdDialog: false,
      columns: [
        { config: { type: 'index', width: 100 }, label: '序号' },
        { dataIndex: 'title', label: '资讯标题'},
        { dataIndex: 'content', label: '简介', slots: true},
        { dataIndex: 'created_at', label: '创建时间' },
        { dataIndex: 'status', label: '是否启用', slots: true },
        { dataIndex: 'oper', label: '操作', slots: true }
      ],
      roleList: [],
      url_type:'1',
      markId: '',
      ruleForm: {
        title: [
          { required: true, message: '请输入资讯标题', trigger: 'blur' }
        ],
        scenic_id: [
          { required: true, message: '请选择景区', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ],
        content_html: [
          { required: true, message: '请输入详情内容', trigger: 'blur' }
        ]
      },
      scenicList: []
    }
  },
  created() {
    this.getScenicList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      tourScenicActicleGetListByCon( {town_id:this.town_id,page: this.page}).then(res => {
        if (this.pagination) {
          const { data, total } = res.data
          this.total = total
          this.dataSource = data
          this.cacheDataSource = JSON.parse(JSON.stringify(data))
        } else {
          this.dataSource = res.data
          this.cacheDataSource = JSON.parse(JSON.stringify(res.data))
        }
        this.handleSetCancelStatus?.()
        // this.handleCompleteTable()
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 获取景区列表
     */
    getScenicList() {
      tourScenicGetListByCon({town_id:this.town_id,level:'label'}).then(res => {
        this.scenicList = res.data.data
      })
    },
    /**
     * 新增/编辑人员
     */
    createManager(idSend) {
      if(Object.keys(idSend).length) {
        tourScenicActicleEdit({ ...this.formInfo,...idSend,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }else{
        tourScenicActicleCreate({ ...this.formInfo,town_id: this.town_id })
          .then((res) => {
            if (res.code === 200) {
              this.$message.success(this.isAdd ? '添加成功' : '编辑成功')
              this.formInfo = {}
              this.formVisible = false
              this.handleGetTable()
            }
          }).finally(() => {
          this.handleResetDialog()
        })
      }
    },
    /**
     * 更改人员状态
     */
    changeStatus(id,status) {
      tourScenicActicleSetStatus({
        tour_scenic_acticle_id: id,
        status
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.handleGetTable()
        }
      })
    },
    /**
     * 编辑
     * @param item
     */
    handleEit(item) {
      let { id, scenic_id, title, content, content_html } = item
      this.markId = 'tour_scenic_acticle_id'
      this.handleOpenForm(id,['scenic_id','title','content','content_html'],{scenic_id,title,content,content_html})
    },
    /**
     * 删除
     * @param admin_ids
     */
    handleDelete(tour_scenic_acticle_ids) {
      tourScenicActicleDestroy({ tour_scenic_acticle_ids }).then((res) => { if (res.code === 200) {
        this.$message.success(res.msg)
        this.handleGetTable()
      } })
    },
    /**
     * 监控上传数量
     */
    overMax() {
      this.$message.warning({ message: '上传图片最大为1张' })
    },
    /**
     * 得到上传成功的文件
     * @param fileList
     * @param type
     */
    getFile(fileList,type) {
      this.formInfo.img = fileList

    },
    /**
     * 删除图片
     * @param fileList
     */
    deleteFile(fileList) {
      this.formInfo.img = fileList
    },
    /**
     * 变化清空
     */
    handleEmpty() {
      this.$set(this.formInfo,'target','')
    },
    /**
     * 翻页
     * @param val
     */
    handlePage(val) {
      this.page = val
    }
  }
}
</script>
<style lang="scss" src="./style/index.scss" scoped/>
<style scoped>
.m-l-16 {
  margin-left: 16px;
}
.up-load {
  height: 174px;
}
</style>
